import dt from '@dt/dt'
import server from '@dt/server/dt'

const api = {
  list: server.api().post('/dt-building/data/list'),
  add: server.api().post('/dt-building/data/create'),
  edit: server.api().post('/dt-building/data/update'),
  remove: server.api().post('/dt-building/data/del')
}

function list(data) {
  data.housesId = dt.buildInfo().housesId
  data.pageNum = data.page
  data.pageSize = data.rows
  return api.list.fetch(data).then((res) => {
    res.list = res.records.map((itm) => {
      itm.attachList = itm.buAttaches.map((att) => {
        return { imageName: att.fileName, imageURL: att.attachAddress }
      })
      return itm
    })
    return res
  })
}

function edit(data) {
  data.attachList = data.attachList.map((itm) => {
    return { imageName: itm.imageName, imageURL: itm.imageURL }
  })
  let func = data.housesDataId ? api.edit : api.add
  return func.fetch(data)
}

function remove(housesDataId) {
  return api.remove.fetch({ housesDataId })
}

export default {
  list,
  edit,
  remove
}
